package com.chinasoft04.mall.controller;

import com.chinasoft04.mall.pojo.*;
import com.chinasoft04.mall.pojo.*;
import com.chinasoft04.mall.service.EchartsService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@AllArgsConstructor
@Slf4j
@CrossOrigin
@RequestMapping("/api/private/v1")
public class EchartsController {

    EchartsService echartsService;


    @GetMapping("/reports/type/1")
    public Rbody getEcharts() {

        //新建了一个最大的data  RBody=data(AllData)+Meta
        Rbody rbody = new Rbody();

        //新建了RBody下的data  allData=Legend+Series+XAxis+YAxis
        AllData allData = new AllData();

        //新建了legend下的data  这个data是area的一个数组
        Legend legend = new Legend();


        //获取地区
        ArrayList<String> objects = new ArrayList<>();
        List<Report> area = echartsService.getArea();
//        for (int i = 0; i < area.size(); i++) {
//            System.out.println(area.get(i).getRp1_area());
//            objects.add(area.get(i).getRp1_area());
//            List<Integer> integerList = echartsService.getData(area.get(i).getRp1_area());
//            series.setData(integerList);
//            series.setName(area.get(i).getRp1_area());
//        }
        List<Series> series1 = new ArrayList<>();
        for (Report report: area) {
            System.out.println(report.getRp1_area());
            objects.add(report.getRp1_area());
            String rp1_area = report.getRp1_area();
        }

        for (int i = 0; i < area.size(); i++) {
            Series series = new Series();

//            System.out.println("!!!!"+area.get(i).getRp1_area());
            series.setName(area.get(i).getRp1_area());
                        List<Integer> integerList = echartsService.getData(area.get(i).getRp1_area());
//            System.out.println("=============="+integerList);
            series.setData(integerList);
//            System.out.println("@@@@@@@@@"+series);

            series1.add(series);
        }
//        System.out.println("%%%%%%%%%"+series1);


        System.out.println("area is " + area);
        System.out.println("objects is " + objects);

        //把拿到的数组给legend
        legend.setData(objects);
        System.out.println("给legend" + legend.getData());

        //把legend给allData
        allData.setLegend(legend);

        allData.setSeries(series1);

        //yAxis
        YAxis yAxis = new YAxis();
        yAxis.setType("value");
        ArrayList<YAxis> yAxisArrayList = new ArrayList<>();
        yAxisArrayList.add(yAxis);
        allData.setYAxis(yAxisArrayList);

        //xAxis
        XAxis xAxis = new XAxis();
        ArrayList<Object> objects1 = new ArrayList<>();
        List<Report> date = echartsService.getDate();
        for (int i=0;i<date.size();i++){
            Date rp1_date = date.get(i).getRp1_Date();
            SimpleDateFormat format2 = new SimpleDateFormat("yyyy-MM-dd");
            String format = format2.format(rp1_date);
            objects1.add(format);
        }
        System.out.println("objects1 is"+objects1);

        //把拿到的数据给xAxis
        xAxis.setData(objects1);
        ArrayList<XAxis> xAxisArrayList = new ArrayList<>();
        xAxisArrayList.add(xAxis);

        System.out.println("/////"+xAxisArrayList);

        allData.setXAxis(xAxisArrayList);
        rbody.getMeta().setStatus(200);
        rbody.getMeta().setMsg("success");

        //把allData给RBody
        rbody.setData(allData);

//        String s = JSON.toJSONString(allData);
//        rbody.setData(s);

        return rbody;

    }

}
